package other.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;

public class MyHttp {

    public static void main(String[] args) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(Inet4Address.getByName("127.0.0.1"),8081));
            String message1 ="Get / HTTP/1.1\r\n"+
                             "Host: www.baidu.com\r\n"+
                             "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\n"+
                             "Accept-Encoding: gzip, deflate, br\r\n"+
                             "Accept-Language: zh-CN,zh;q=0.9\r\n"+
                             "\r\n";
            String message2 ="Get /dept/list HTTP/1.1\r\n"+
                            "Host: localhost:8081\r\n"+
                            "Connection: keep-alive\r\n"+
                            "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\n"+
                            "Accept-Encoding: gzip, deflate, br\r\n"+
                            "Accept-Language: zh-CN,zh;q=0.9\r\n"+
                            "\r\n";
            System.out.println(message2);
            OutputStream  outputStream = socket.getOutputStream();
            outputStream.write(message2.getBytes());
            InputStream inputStream =  socket.getInputStream();
            byte[] bys = new byte[256];
            int len=0;
            while ((len = inputStream.read(bys))!=-1){
                System.out.println(new String(bys,0,len));
            }
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
